//
//  HomeContentView.m
//  GuoBuGaForIphone
//
//  Created by zhangwo on 16/10/26.
//  Copyright © 2016年 GuoBuGa. All rights reserved.
//

#import "HomeContentView.h"

@interface HomeContentView ()
{
    BOOL _edit;
    UIView *viewBtn;
}

@end

@implementation HomeContentView

@synthesize collectionView = _collectionView;
@synthesize cellSize  = _cellSize;
@synthesize imageSize = _imageSize;
@synthesize dataList  = _dataList;
@synthesize isEditing = _isEditing;
@synthesize delegate  = _delegate;

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height) collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.scrollEnabled = NO;
        _collectionView.backgroundColor = [UIColor clearColor];
        
        [_collectionView registerClass:[HomeContentViewCell class] forCellWithReuseIdentifier:@"cellItem"];
        [self addSubview:_collectionView];
        
        UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(changeState:)];
        [self addGestureRecognizer:longPress];
    }
    return self;
}


- (void)setDataList:(NSArray *)dataList
{
    _dataList = dataList;
    [_collectionView reloadData];
}

- (void)changeState:(UILongPressGestureRecognizer *)press{
    _isEditing = YES;
    [_collectionView reloadData];
    [self removeGestureRecognizer:press];
    if ([_delegate respondsToSelector:@selector(homeContentViewDidBeginEditing:)]) {
        [_delegate homeContentViewDidBeginEditing:self];
    }
}

#pragma mark - collectionView delegate


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return [_dataList count];
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return _cellSize;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 0.001;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 0.001;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    HomeContentViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellItem" forIndexPath:indexPath];
    NSDictionary *data = [_dataList objectAtIndex:[indexPath row]];
    cell.imageSize = _imageSize;
    cell.data = data;
    [self setCellVibrate:cell IndexPath:indexPath];//显示隐藏按钮
    
    cell.layer.borderColor = [UIColor colorWithHexString:@"0xC7C7C7"].CGColor;
    cell.layer.borderWidth = 0.3;
    
    if (_isEditing) {
        if ([[data objectForKey:@"selectable"] boolValue] == 1) {
            //[cell.button removeFromSuperview];
            [cell.button setHidden:NO];
            [cell.button setUserInteractionEnabled:YES];
            [cell.button addTarget:self action:@selector(changeSelect:) forControlEvents:UIControlEventTouchUpInside];
        }else {
            [cell.button setHidden:YES];
        }
    }else {
        [cell.button setHidden:YES];
    }

    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSMutableDictionary *data = [_dataList objectAtIndex:[indexPath row]];
    if (!_isEditing) {
        if (_delegate && [_dataList objectAtIndex:indexPath.row]) {
            [_delegate homeContentView:self didSelectedItem:data];
        }
    }
}


////删除
-(void)deleteCellAtIndexpath:(NSIndexPath *)indexPath cellView:(HomeContentViewCell *)cell
{
//    //performBatchUpdates允许多个插入/删除/重新加载/移动调用同时动画
//    [self.collectionView performBatchUpdates:^{
//        if ([_dataList count]>1) {
//            [_dataList removeObjectAtIndex:indexPath.row];//移除数据源中数据
//            [self.collectionView deleteItemsAtIndexPaths:@[indexPath]];//移除collectionView中数据
//        }
//    } completion:^(BOOL finished) {
//        [self.collectionView reloadData];
//    }];
}

- (void)changeSelect:(UIButton *)button{
    if(button.isSelected){
        [button setSelected:NO];
    }else{
        [button setSelected:YES];
    }
}

//显示隐藏按钮 是否删除
- (void)setCellVibrate:(HomeContentViewCell *)cell IndexPath:(NSIndexPath *)indexPath{
    cell.indexPath = indexPath;
    cell.button.hidden = _deleteBtnFlag?YES:NO;
    cell.delegate = self;
}


@end
